﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

using System;
using System.Text;
using System.IO;

using YTS.Log;

using IOFileServer.ZMPhoneticEnglish;
using IOFileServer.ZMPhoneticEnglish.Implementation;

namespace IOFileServer.Test.ZMPhoneticEnglish
{
    [TestClass]
    public class TestIVowel
    {
        private ILog log;

        [TestInitialize]
        public void Init()
        {
            FileInfo fileInfo = new FileInfo($"./logs/TestIVowel/{DateTime.Now:yyyy_MM_dd}.log");
            log = new FilePrintLog(fileInfo, Encoding.UTF8);
        }

        [TestCleanup]
        public void Clean()
        {
        }

        [TestMethod]
        public void Test_Vowel_CalcPosition()
        {
            IVowel handle;
            string word;
            int[][] position;

            handle = new Vowel();

            #region 测试自编单词: cbulterlate
            // d = "0123456789";
            word = "cbulterlate";

            position = handle.CalcPosition(word);

            Assert.AreEqual(3, position.Length);
            AssertExtend.Check("[2]", position, 0);
            AssertExtend.Check("[5,6]", position, 1);
            AssertExtend.Check("[8]", position, 2);
            #endregion

            #region 单词: table
            // d = "01234";
            word = "table";
            position = handle.CalcPosition(word);
            Assert.AreEqual(1, position.Length);
            AssertExtend.Check("[1]", position, 0);
            #endregion
        }

        [TestMethod]
        public void Test_Vowel_ToRule()
        {
            Vowel handle = new Vowel();
            string expression;
            Vowel.Rule r;

            expression = "a";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(1, r.letter.Length);
            Assert.AreEqual('a', r.letter[0]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNull(r.after_content);

            expression = "ea";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('e', r.letter[0]);
            Assert.AreEqual('a', r.letter[1]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNull(r.after_content);

            expression = "(w)a";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(1, r.letter.Length);
            Assert.AreEqual('a', r.letter[0]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNull(r.after_content);

            expression = "(wu)au";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('a', r.letter[0]);
            Assert.AreEqual('u', r.letter[1]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(2, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.AreEqual('u', r.before_content[1]);
            Assert.IsNull(r.after_content);

            expression = "i(nd)";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(1, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(2, r.after_content.Length);
            Assert.AreEqual('n', r.after_content[0]);
            Assert.AreEqual('d', r.after_content[1]);

            expression = "ie-";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNull(r.after_content);

            expression = "-ie-";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNull(r.after_content);

            expression = "-ie";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNull(r.after_content);

            expression = "-(w)ie";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNull(r.after_content);

            expression = "ie(w)-";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('w', r.after_content[0]);

            expression = "-(w)ie(r)-";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('r', r.after_content[0]);

            expression = "(w)-ie";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsFalse(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNull(r.after_content);

            expression = "ie-(w)";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsFalse(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNull(r.before_content);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('w', r.after_content[0]);

            expression = "-(w)ie-(r)";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('r', r.after_content[0]);

            expression = "(w)-ie(r)-";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('r', r.after_content[0]);

            expression = "(w)-ie-(r)";
            r = handle.ToRule(expression);
            Assert.IsNotNull(r.letter);
            Assert.AreEqual(2, r.letter.Length);
            Assert.AreEqual('i', r.letter[0]);
            Assert.AreEqual('e', r.letter[1]);
            Assert.IsTrue(r.is_before_must);
            Assert.IsTrue(r.is_after_must);
            Assert.IsNotNull(r.before_content);
            Assert.AreEqual(1, r.before_content.Length);
            Assert.AreEqual('w', r.before_content[0]);
            Assert.IsNotNull(r.after_content);
            Assert.AreEqual(1, r.after_content.Length);
            Assert.AreEqual('r', r.after_content[0]);
        }
    }
}
